			
	    	var alumni,grades,alumni_grade, career_subject;
	    	var mayorTable,grouped_subjects,alumniTable;
	    	var lineChart;
	    	var lineChartData;
	    	var grouped_subjects;
	    	var dataViz;
			var minYear = 1999;
			var maxYear = 2012;
			var minViewYear = 1999;
			var maxViewYear = 2012;
			var min_avg = 0;
			var max_avg = 10;
			var annotatedtimeline;
			var viz = 0;
			var allGradesCol=0;
			var minGradeCol=0;
	    	// Load the Visualization API and the piechart package.
	    	google.load('visualization', '1.0', {packages:['table','controls','corechart']});
	      
	    	// Set a callback to run when the Google Visualization API is loaded.
	    	google.setOnLoadCallback(initialize);
		    
	    	function initialize() {
        		var query = new google.visualization.Query('https://docs.google.com/spreadsheet/pub?key=0Ar2w8seFCoUYdHdDbGZ1bUk4cVU4SGdDUWlvSXhCOGc&output=csv');
        		query.setTimeout(30);
        		query.send(handleQueryResponseForAlumni);
        	}
        
    		function handleQueryResponseForAlumni(response) {
    			if (response.isError()) {
        			alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
        			return;
        		}
	  
        		alumni = response.getDataTable();
				var query = new google.visualization.Query('https://docs.google.com/spreadsheet/pub?key=0Ar2w8seFCoUYdHIxa2Y4TnFoeG13dEFwLXduYXYyQVE&output=csv');
        		query.setTimeout(30);
	        	query.send(handleQueryResponseForGrades);
	        }
	        
	        function handleQueryResponseForGrades(response) {
		        if (response.isError()) {
			        alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
	        		return;
	        	}
		  
	        	grades = response.getDataTable();       	
				alumni_grade=google.visualization.data.join(grades, alumni,'inner',[[0,0]],[1,2,3,4],[1,2,3]);
				career_subject=google.visualization.data.join(grades, alumni,'inner',[[0,0]],[2],[2]);
				career_subject = google.visualization.data.group(career_subject, [1, 2]);
	        	buildSubjectTable();
	        	buildAlumniTable();	    
	        	buildCareerTable();	         
				initVisualization();
				printViz();
				buildChartControls()
	        }
	        
	        function buildCareerTable() {
	        	var stringFilter = new google.visualization.ControlWrapper({
	                'controlType': 'StringFilter',
	                'containerId': 'career_filter',
	                'options': {
	                  'filterColumnLabel': 'Carrera',
	                  'ui' : {'label': 'Filtrar por Nombre'}
	                }
	              });	        	
	        	grouped_Career = google.visualization.data.group(alumni, [2]);
	        	grouped_Career.setColumnLabel(0, 'Carrera');
	        	grouped_Career.addColumn('boolean', 'Selected');
	        	for(index=0;index<grouped_Career.getNumberOfRows();++index) {
	        		grouped_Career.setCell(index,1,false);
	        	}	        		        	
	        	careerTable = new google.visualization.ChartWrapper({
	                 'chartType': 'Table',
	                 'containerId': 'career_table',
	                 'options': {'height': '30em', 'width': '40em'}
	               });  
	        	
  				var dashboard = new google.visualization.Dashboard(document.getElementById('dashboard_career')).bind(stringFilter, careerTable).draw(grouped_Career);	
  				google.visualization.events.addListener(careerTable, 'select', selectionEventCareer);
	        }
	        	        
	        function selectionEventCareer() {
		        var item = careerTable.getChart().getSelection()[0];
		        var careerSelected = careerTable.getDataTable().getValue(item.row, 0);
				var previousValue = careerTable.getDataTable().getFormattedValue(item.row, 1);
				var index = item.row;
				if(careerTable.getDataTable().getNumberOfRows() != grouped_Career.getNumberOfRows()){
					index = getCareerIndex(careerSelected);
				}				
				if(previousValue=='false'){
					grouped_Career.setValue(index, 1,true);
					addCareerToViz(index);
				} else {
					grouped_Career.setValue(index, 1,false);
					removeCareerFromViz(index);
					
				}
				careerTable.draw();
				printViz();		
				careerTable.getChart().setSelection();
	        } 
	        
	        function getCareerIndex(career) {      	
	        	for(index=0;index<grouped_Career.getNumberOfRows();++index) {
	        		if(grouped_Career.getValue(index,0) == career)
	        			return index;
	        	}	        	
	        }
	        
	        function buildAlumniTable() {
	        	var stringFilter = new google.visualization.ControlWrapper({
	                'controlType': 'StringFilter',
	                'containerId': 'alumni_filter_1',
	                'options': {
	                  'filterColumnLabel': 'Alumno',
	                  'ui' : {'label': 'Filtrar por Nombre'}
	                }
	              });
	        	
	        	var avgFilter = new google.visualization.ControlWrapper({
	        		'controlType': 'NumberRangeFilter',
	        	    'containerId': 'alumni_filter_2',
	        	    'options': {
	        	      'filterColumnLabel': 'Avg',
	                  'ui' : {'label': 'Promedio:'},
	        	      'minValue': 0,
	        	      'maxValue': 10
	        	    }               
	              });
	        	
	        	var careerFilter = new google.visualization.ControlWrapper({
	                'controlType': 'StringFilter',
	                'containerId': 'alumni_filter_3',
	                'options': {
	                  'filterColumnLabel': 'Carrera',
	                  'ui' : {'label': 'Filtrar por Carrera'}
	                }
	              });
	        	
	        	grouped_alumni = google.visualization.data.group(alumni, [1, 2]);
	        	var grouped_alumni_aux = google.visualization.data.group(alumni, [1,3]);
	        	grouped_alumni.setColumnLabel(0, 'Alumno');
	        	grouped_alumni.setColumnLabel(1, 'Carrera');
	        	grouped_alumni.insertColumn(1, 'boolean', 'Selected');
	        	grouped_alumni.insertColumn(2, 'date','Egreso');
	        	grouped_alumni.insertColumn(3, 'date','Ingreso');
	        	grouped_alumni.insertColumn(4, 'number','Avg');
	        	for(index=0;index<grouped_alumni.getNumberOfRows();++index) {
	        		grouped_alumni.setCell(index,1,false);
	        		grouped_alumni.setCell(index,2,grouped_alumni_aux.getValue(index,1));
	        		grouped_alumni.setCell(index,3,getEgresDate(grouped_alumni.getValue(index,0)));
	        		grouped_alumni.setCell(index,4,getStudentAverage(grouped_alumni.getValue(index,0)));
	        		//grouped_alumni.setCell(index,3,getStudentAverage(grouped_alumni.getValue(index,0)));
	        		//grouped_alumni.setCell(index,4,getEgresDate(grouped_alumni.getValue(index,0)));
	        	}
	        	alumniTable = new google.visualization.ChartWrapper({
	                 'chartType': 'Table',
	                 'containerId': 'alumni_table',
	                 'options': {'height': '30em', 'width': '40em'},
	                 'view': {'columns': [0, 2, 3, 4, 1]}
	               });  
	        	
	        	var dashboard = new google.visualization.Dashboard(document.getElementById('dashboard_alumni')).bind([stringFilter, avgFilter, careerFilter], alumniTable).draw(grouped_alumni);
  				google.visualization.events.addListener(alumniTable, 'select', selectionEventAlumni);
	        }
	        
	        function getEgresDate(name) {
	        	var la = new Date(2100,1,1);
		        var max=la;
		        for(var i=0;i<alumni_grade.getNumberOfRows();++i) {
		        	if(alumni_grade.getValue(i,5)==name) {
				        var date = alumni_grade.getValue(i,3);
				        if(max > date ) {
					        max = date;
				        }
			        }
		        }
		        if(max==la)
		        	max=null;
		        return max;
	        }
	        
	        function getStudentAverage(name) {
		        var code = getAlumniCode(name);
		        var counter=0;
		        var sum=0;
		        if(code) {
			        for(var i=0;i<grades.getNumberOfRows();++i) {
				        if(grades.getValue(i,0)==code) {
					        sum += grades.getValue(i,4);
					        ++counter;
				        }
			        }
			        if(counter > 0)
			        	return parseFloat((sum/counter).toFixed(2));
			        return 0;
		        }
		        return null;
	        }
	        
	        function getAlumniCode(name) {
		        for(var i=0;i<alumni.getNumberOfRows();++i) {
			        if(alumni.getValue(i,1)==name) {
			        	//alert(alumni.getValue(i,1)+" "+name);
			        	//1/0;
				        return alumni.getValue(i,0);
			        }
		        }
		        return false;
	        }
	        
	        function selectionEventAlumni() {
	        	var item = alumniTable.getChart().getSelection()[0];
		        var alumniSelected = alumniTable.getDataTable().getValue(item.row, 0);
				var previousValue = alumniTable.getDataTable().getFormattedValue(item.row, 1);
				var index = item.row;
				if(alumniTable.getDataTable().getNumberOfRows() != grouped_alumni.getNumberOfRows()){
					index = getAlumniIndex(alumniSelected);
				}				
				if(previousValue=='false'){
					grouped_alumni.setValue(index, 1,true);
					addAlumniToViz(index);
				} else {
					grouped_alumni.setValue(index, 1,false);
					removeAlumniFromViz(index);
					
				}
				alumniTable.draw();
				printViz();		
				alumniTable.getChart().setSelection();
	        }
	        
	        function getAlumniIndex(alumni) {	        	
	        	for(index=0;index<grouped_alumni.getNumberOfRows();++index) {
	        		if(grouped_alumni.getValue(index,0) == alumni)
	        			return index;
	        	}	        	
	        }
	        
	        function buildSubjectTable() {
	        	var stringFilter = new google.visualization.ControlWrapper({
	                'controlType': 'StringFilter',
	                'containerId': 'subject_filter_1',
	                'options': {
	                  'filterColumnLabel': 'Materia',
	                  'ui' : {'label': 'Filtrar por Nombre'}
	                }
	              });
	        	
	        	var careerFilter = new google.visualization.ControlWrapper({
	                'controlType': 'StringFilter',
	                'containerId': 'subject_filter_2',
	                'options': {
	                  'filterColumnLabel': 'Carrera',
	                  'ui' : {'label': 'Filtrar por Carrera'}
	                }
	              });
	        	
	        	grouped_subjects = google.visualization.data.group(grades, [2, 1]);
	 	       	grouped_subjects.setColumnLabel(1, 'Codigo');
	        	grouped_subjects.setColumnLabel(0, 'Materia');
	        	grouped_subjects.insertColumn(1,'boolean', 'Selected');
	        	var careerCol = grouped_subjects.addColumn('string', 'Carrera');   
	        	
	        	for(index=0;index<grouped_subjects.getNumberOfRows();++index) {
	        		grouped_subjects.setCell(index,1,false);
	        		grouped_subjects.setCell(index,careerCol, getCareerOfSubject(grouped_subjects.getValue(index, 0)));
	        	}

	        	subjectTable = new google.visualization.ChartWrapper({
	                 'chartType': 'Table',
	                 'containerId': 'subject_table',
	                 'options': {'height': '30em', 'width': '40em'},
	                 'view': {'columns': [2, 0, 1]}
	               });  
	        	
	        	var dashboard = new google.visualization.Dashboard(document.getElementById('dashboard_subject')).bind([stringFilter, careerFilter], subjectTable).draw(grouped_subjects);
  				google.visualization.events.addListener(subjectTable, 'select',selectionEventSubjects);
	        }
	        
	        function getCareerOfSubject(subject){
	        	for (i = 0; i < career_subject.getNumberOfRows(); i++) {
 					if((career_subject.getValue(i, 0) == subject)){	
						return career_subject.getValue(i, 1);
 					}	
				}
	        }
	        
	        function selectionEventSubjects() {
	        	var item = subjectTable.getChart().getSelection()[0];
		        var subjectSelected = subjectTable.getDataTable().getValue(item.row, 0);
				var previousValue = subjectTable.getDataTable().getFormattedValue(item.row, 1);
				var index = item.row;
				if(subjectTable.getDataTable().getNumberOfRows() != grouped_subjects.getNumberOfRows()){
					index = getSubjectIndex(subjectSelected);
				}				
				if(previousValue=='false'){
					grouped_subjects.setValue(index, 1,true);
					addSubjectToViz(index);
				} else {
					grouped_subjects.setValue(index, 1,false);
					removeSubjectFromViz(index);
					
				}
				subjectTable.draw();
				printViz();		
				subjectTable.getChart().setSelection();
	        }
	        
	        function getSubjectIndex(subject) {        	
	        	for(index=0;index<grouped_subjects.getNumberOfRows();++index) {
	        		if(grouped_subjects.getValue(index,0) == subject)
	        			return index;
	        	}	        	
	        }
	        
	        function initVisualization() {
				if(viz == 0){
					initLineVisualization();
				} else {
					initDotVisualization();
				}	        	
	          }
	        
	        function reloadVisualization() {
	        	initVisualization()
	        	for(index=0;index<grouped_Career.getNumberOfRows();index++) {
        			if(grouped_Career.getValue(index, 1) == true){
						addCareerToViz(index);
        			}
        		}
	        	for(index=0;index<grouped_subjects.getNumberOfRows();index++) {
        			if(grouped_subjects.getValue(index, 1) == true){
						addSubjectToViz(index);
        			}
        		}
				
	        	for(index=0;index<grouped_alumni.getNumberOfRows();index++) {
        			if(grouped_alumni.getValue(index, 1) == true){
						addAlumniToViz(index);
        			}
        		}
				printViz();		            
	          }

	        
	        function cantRows(){
	        	return (maxYear - minYear)*12;
	        }
	        
	        function sameMonth(dateA , dateB){
	        	if(dateA.getFullYear() == dateB.getFullYear() && dateA.getMonth() == dateB.getMonth()){
        			return true;
	        	}	        	
	        	return false;   	
	        }
	        
	        function validDate(date){
	        	if(date.getFullYear() <= maxYear && date.getFullYear() >= minYear){
        			return true;
	        	}	        	
	        	return false;   	
	        }
	        function validGrade(grade){
	        	if(grade >= 0 && grade  <= 10){
        			return true;
	        	}	        	
	        	return false;   	
	        }
	        
	        function addCareerToViz(i) {
				if(viz == 0){
					addCareerToLineViz(i);
				} else {
					addCareerToDotViz(i);
				}
	        }
	         
	        
	        function addAlumniToViz(i) {
				if(viz == 0){
					addAlumniToLineViz(i);
				} else {
					addAlumniToDotViz(i);
				}	           
	        }
	        
	        
	        function addSubjectToViz(i) {
				if(viz == 0){
					addSubjectToLineViz(i);
				} else {
					addSubjectToDotViz(i);
				}            
	        }
	        
			function removeCareerFromViz(i) {
				for(row = 1; row < dataViz.getNumberOfColumns(); row ++) {
					if(dataViz.getColumnLabel(row) == grouped_Career.getValue(i,0))
						dataViz.removeColumn(row);
				}           
	        }
			
	        function removeAlumniFromViz(i) {
				for(row = 1; row < dataViz.getNumberOfColumns(); row ++) {
					if(dataViz.getColumnLabel(row) == grouped_alumni.getValue(i,0))
						dataViz.removeColumn(row);
				}            
	        }
	        

			function removeSubjectFromViz(i) {
				for(row = 1; row < dataViz.getNumberOfColumns(); row ++) {
					if(dataViz.getColumnLabel(row) == grouped_subjects.getValue(i,0))
						dataViz.removeColumn(row);
				}            
	        }
			
			function printViz(){
	      		if(dataViz.getNumberOfColumns() <= 1)
	      			return;
	      		
				if(viz == 0){
					printLineViz();
				} else {
					printDotViz();
				}
					
			}
			
			function removeMinGrade(){
				dataViz.removeColumn(minGradeCol);
				printViz();	
			}
			
			function addMinGrade(){
				if(viz == 0){
					addMinGradeInLine();
				} else {
					addMinGradeInDot();
				}
				printViz();	
			}
			
			function removeAllGrades(){
				dataViz.removeColumn(allGradesCol);
				printViz();	
			}
			
			function addAllGrades(){
				if(viz == 0){
					addAllGradesInLine();
				} else {
					addAllGradesInDot();
				}
				printViz();	
			}
			
			function correctDate(dateA , dateB){
				if(dateA == null || dateB == null)
					return true;
				if(viz == 0){
					return sameMonth(dateA , dateB);
				} else {
					return (dateA.getFullYear() == dateB.getFullYear() && dateA.getMonth() == dateB.getMonth() && dateA.getDate() == dateB.getDate() );
				} 	
	        }
			
			function setInfoDate(date){
				var month = date.getMonth()+1;
				if(viz == 0){
					return month +"/"+ date.getFullYear();
				} else {
					return date.toLocaleDateString();
				} 	
	        }			
			
			
			function cleanAllDataSelected(){
				for(index=0;index<grouped_Career.getNumberOfRows();index++) {
	        		grouped_Career.setCell(index,1,false);
	        	}	        	
        		
	        	for(index=0;index<grouped_subjects.getNumberOfRows();index++) 
	        		grouped_subjects.setValue(index, 1, false);
				
	        	for(index=0;index<grouped_alumni.getNumberOfRows();index++) 
	        		grouped_alumni.setValue(index, 1, false);
	        	
	        	initVisualization();

	        	careerTable.getChart().draw(grouped_Career,null);
				subjectTable.draw(grouped_subjects,null);
				alumniTable.draw(grouped_alumni,null);
	        	
				printViz();	
				
			}
			 function selectDataFromChart() {
			        var item = annotatedtimeline.getSelection()[0];
			        if(item == null)
			        	return;
			        
			        var info="";
			        if(item.row != null){
				        var fecha = dataViz.getValue(item.row, 0);
				        info+="<br/><b>Fecha</b>: " + setInfoDate(fecha) + "<br/>";
			        }
			        var name = dataViz.getColumnLabel(item.column);
			        if(isSubject(name)) {
				        info+="<b>Nombre de la materia</b>: "+name+ "<br/>";
				        info+=getSubjectInfo(name, fecha);
			        }
			        else if(isAlumni(name)) {
				        info+="<b>Nombre del alumno</b>: "+name+ "<br/>";
				        info+=getAlumniInfo(name, fecha);
			        }
			        else if(isCareer(name)){
				        info+="<b>Nombre de la carrera</b>: "+name+ "<br/>";
				        info+=getCareerInfo(name, fecha);
			        }			        
			        document.getElementById("information").innerHTML=info;
			        
		        }
			 
			  function isSubject(name) {
			        for(var i=0;i<grouped_subjects.getNumberOfRows();++i) {
				        if(grouped_subjects.getValue(i,0)==name) {
					        return true;
				        }
			        }
			        return false;
		        }
		        
		        function isAlumni(name) {
			        for(var i=0;i<grouped_alumni.getNumberOfRows();++i) {
				        if(grouped_alumni.getValue(i,0)==name) {
					        return true;
				        }
			        }
			        return false;
		        }
		        
		        function isCareer(name) {
			        for(var i=0;i<grouped_Career.getNumberOfRows();++i) {
				        if(grouped_Career.getValue(i,0)==name) {
					        return true;
				        }
			        }
			        return false;
		        }
		        
		        function getSubjectInfo(name, date) {
		        //	if(date == null)
		        	//	return "";
		        	var info = "<table border=\"1\"><tr><th>Alumno</th><th>Nota</th><th>Fecha</th></tr>";		        	
		        	for(var i=0;i<alumni_grade.getNumberOfRows();++i) {
				        if(alumni_grade.getValue(i,2)==name && correctDate(alumni_grade.getValue(i,3), date)) {
				        	info+="<tr><td>"+alumni_grade.getValue(i,5)+"</td><td>"+alumni_grade.getValue(i,4)+"</td><td>"+ alumni_grade.getValue(i,3).toLocaleDateString()+"</td></tr>";
				        }
			        }
		        	info+="</table>";		        	
		        	return info;
		        }
		        
		        function getAlumniInfo(name, date) {
		       //	if(date == null)
		        //		return "";
		        	var info = "<table border=\"1\"><tr><th>Materia</th><th>Nota</th><th>Fecha</th></tr>";		        	
		        	for(var i=0;i<alumni_grade.getNumberOfRows();++i) {
				        if(alumni_grade.getValue(i,5)==name && correctDate(alumni_grade.getValue(i,3), date)) {
				        	info+="<tr><td>"+alumni_grade.getValue(i,2)+"</td><td>"+alumni_grade.getValue(i,4)+"</td><td>"+ alumni_grade.getValue(i,3).toLocaleDateString()+"</td></tr>";
				        }
			        }
		        	info+="</table>";		        	
		        	return info;
		        }
		        
		        function getCareerInfo(name, date) {
		       /// 	if(date == null)
		       // 		return "";
		        	var info = "<table border=\"1\"><tr><th>Materia</th><th>Alumno</th><th>Nota</th><th>Fecha</th></tr>";		
		        	for (var i = 0; i < career_subject.getNumberOfRows(); i++) {
	 					if((career_subject.getValue(i, 1) == name)){					
						for (var j = 0; j < alumni_grade.getNumberOfRows(); j++) {					
							if((alumni_grade.getValue(j, 2) == career_subject.getValue(i, 0)) && correctDate(date, alumni_grade.getValue(j, 3))){
								info+="<tr><td>"+alumni_grade.getValue(j, 2)+"</td><td>"+alumni_grade.getValue(j, 5)+"</td><td>"+alumni_grade.getValue(j, 4)+"</td><td>"+ alumni_grade.getValue(j, 3).toLocaleDateString()+"</td></tr>";
							}
							
						}
	 					}
		        	}
		        	info+="</table>";	
		        	return info;
		        }     	
		        
	        